package com.askey.bitratedemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, EasyPermissions.PermissionCallbacks {
    private EditText mW;
    private EditText mH;
    private EditText mB;
    private EditText mF;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cameraPermission();

        ((Button) findViewById(R.id.btn_start)).setOnClickListener(this);
        mW = (EditText) findViewById(R.id.et_width);
        mH = (EditText) findViewById(R.id.et_height);
        mB = (EditText) findViewById(R.id.et_br);
        mF = (EditText) findViewById(R.id.et_fr);

        CAMERA.getFpsRange(this, CameraCharacteristics.LENS_FACING_BACK);
    }

    private static final int RC_CAMERA_PERM = 123;

    @AfterPermissionGranted(RC_CAMERA_PERM)
    public void cameraPermission() {
        if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA)) {
        } else {
            EasyPermissions.requestPermissions(this, "Request Camera Permission", RC_CAMERA_PERM, Manifest.permission.CAMERA);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        Toast.makeText(this, "onPermissionsGranted", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        Toast.makeText(this, "onPermissionsDenied", Toast.LENGTH_SHORT).show();
        new AppSettingsDialog.Builder(this)
                .setTitle("权限申请")
                .setPositiveButton("确认")
                .setNegativeButton("取消")
                .setRationale("当前App需要申请camera权限,需要打开设置页面么?")
                .setRequestCode(RC_CAMERA_PERM)
                .build()
                .show();
    }

    @Override
    public void onClick(View v) {
        try {
            int w = Integer.parseInt(mW.getText().toString());
            int h = Integer.parseInt(mH.getText().toString());
            int br = Integer.parseInt(mB.getText().toString());
            int fr = Integer.parseInt(mF.getText().toString());
            int mode = ((Spinner) findViewById(R.id.mode)).getSelectedItemPosition();

            Intent i = new Intent();
            i.setClass(MainActivity.this, BitrateChartActivity.class);
            i.putExtra("w", w);
            i.putExtra("h", h);
            i.putExtra("br", br);
            i.putExtra("fr", fr);
            i.putExtra("mode", mode);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
        } catch (NumberFormatException e) {
            Toast.makeText(this, "Illegal Value.", Toast.LENGTH_SHORT).show();
        }
    }
}
